package cn.blackjuly.cadmin.config;

import cn.blackjuly.cadmin.constant.WebAppConstant;
import cn.blackjuly.cadmin.controller.MgtController;
import cn.blackjuly.cadmin.controller.SettingController;
import cn.blackjuly.cadmin.controller.UploadController;
import cn.blackjuly.cadmin.model._MappingKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;

import cn.blackjuly.cadmin.controller.IndexController;

public class WebAppConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		me.setViewExtension(".jsp");
		me.setJsonDatePattern("yyyyMMdd HH:mm:dd");
		me.setError404View("/WEB-INF/template/404.jsp");
		me.setBaseUploadPath(WebAppConstant.BASE_UPLOAD_PATH);
	}

	@Override
	public void configRoute(Routes me) {
		me.setBaseViewPath("/WEB-INF/template");
		me.add("/", IndexController.class);
		me.add("/upload", UploadController.class);
		me.add("/admin", MgtController.class);
		me.add("/admin/setting", SettingController.class);
	}

	@Override
	public void configEngine(Engine me) {
	}

	@Override
	public void configPlugin(Plugins me) {
		PropKit.use("application.properties");
		String url = PropKit.get("datasource.url");
		String username = PropKit.get("datasource.username");
		String password = PropKit.get("datasource.password");
		String driverClass = PropKit.get("datasource.driveClass");

		DruidPlugin druidPlugin = new DruidPlugin(url, username, password, driverClass);
		me.add(druidPlugin);

		ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
		_MappingKit.mapping(activeRecordPlugin);
		me.add(activeRecordPlugin);
	}

	@Override
	public void configInterceptor(Interceptors me) {
	}

	@Override
	public void configHandler(Handlers me) {
	}
}